//
//  LYD_FMDBGetDatabase.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/11/12.
//  Copyright © 2018 蒂埃里. All rights reserved.
//

#import "LYD_FMDBGetDatabase.h"

@implementation LYD_FMDBGetDatabase

@synthesize dbQueue = _dbQueue;
static LYD_FMDBGetDatabase *database = nil;

//单例
+ (instancetype)shareTool_FMDBGetDatabase
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        database = [[self alloc] init] ;
    }) ;
    return database;
}
- (id)copyWithZone:(struct _NSZone *)zone
{
    return [LYD_FMDBGetDatabase shareTool_FMDBGetDatabase];
}

//创建数据库保存路径
+ (NSString *)dbPath
{
    NSString *docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSFileManager *filemanage = [NSFileManager defaultManager];
    docsdir = [docsdir stringByAppendingPathComponent:@"FMDB"];
    BOOL isDir;
    BOOL exit =[filemanage fileExistsAtPath:docsdir isDirectory:&isDir];
    if (!exit || !isDir) {
        [filemanage createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *dbpath = [docsdir stringByAppendingPathComponent:@"LYD_FMDB.sqlite"];
    return dbpath;
}

//创建多线程安全的数据库
- (FMDatabaseQueue *)dbQueue
{
    if (_dbQueue == nil) {
        _dbQueue = [[FMDatabaseQueue alloc] initWithPath:[self.class dbPath]];
    }
    return _dbQueue;
}


@end
